package com.nari.ncps.screen.service.impl.ne;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nari.ncps.screen.entity.ne.NePopupSafe;
import com.nari.ncps.screen.entity.ne.NeProdoperElectricEff;
import com.nari.ncps.screen.mapper.ne.NePopupSafeMapper;
import com.nari.ncps.screen.service.ne.NePopupSafeService;
import com.nari.ncps.screen.vo.ne.WindSafetyEnvVO;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 新能源弹窗安全环保 服务实现类
 * </p>
 *
 * @author dongxiaoxu
 * @since 2025-11-04
 */
@Service
public class NePopupSafeServiceImpl extends ServiceImpl<NePopupSafeMapper, NePopupSafe> implements NePopupSafeService {

    @Override
    public WindSafetyEnvVO getWindSafetyEnv(String orgCode) {
        if (orgCode == null) {
            orgCode = "134058";
        }
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
// 获取七天前的日期
        LocalDate sevenDaysAgo = currentDate.minusDays(6); // 包含当天，共7天
        // 格式化为 yyyyMMdd
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        String current = currentDate.format(formatter);
        String sevenDaysAgoStr = sevenDaysAgo.format(formatter);
        LambdaQueryWrapper<NePopupSafe> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(NePopupSafe::getDelFlag, 0)
                .eq(NePopupSafe::getOrgCode, orgCode)
                .eq(NePopupSafe::getDateType, 2)
                .ge(NePopupSafe::getStatisticTime, sevenDaysAgoStr)
                .le(NePopupSafe::getStatisticTime, current);
        List<NePopupSafe> nePopupSafeList = this.list(queryWrapper);
//        节约标煤趋势数据
        List<WindSafetyEnvVO.CoalSavingItem> coalSavingItemList = new ArrayList<>();
//        二氧化碳减排趋势数据
        List<WindSafetyEnvVO.Co2ReductionItem> co2ReductionItemList = new ArrayList<>();
        for (NePopupSafe nePopupSafe : nePopupSafeList) {
            WindSafetyEnvVO.CoalSavingItem coalSavingItem = new WindSafetyEnvVO.CoalSavingItem();
            WindSafetyEnvVO.Co2ReductionItem co2ReductionItem = new WindSafetyEnvVO.Co2ReductionItem();
            coalSavingItem.setDate(nePopupSafe.getStatisticTime());
            coalSavingItem.setCoalSaving(nePopupSafe.getStandardCoalSaving());
            co2ReductionItem.setCo2Reduction(nePopupSafe.getCo2EmissionReduction());
            co2ReductionItem.setDate(nePopupSafe.getStatisticTime());
            coalSavingItemList.add(coalSavingItem);
            co2ReductionItemList.add(co2ReductionItem);
        }
        WindSafetyEnvVO vo = new WindSafetyEnvVO();
        vo.setCo2ReductionTrend(co2ReductionItemList);
        vo.setCoalSavingTrend(coalSavingItemList);
        return vo;
    }
}
